package com.ruicar.afs.cloud.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.product.condition.ProductRuleInfoCondition;
import com.ruicar.afs.cloud.product.entity.ProductRuleInfo;
import com.ruicar.afs.cloud.product.service.ProductRuleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author yaorui.zhang
 * @date 2020年5月14日11:29:42
 */
@RestController
@AllArgsConstructor
@RequestMapping("/productRuleInfo")
@Api(value = "productRuleInfo", description = "产品规则信息")
public class ProductRuleInfoController {

    private final ProductRuleInfoService service;

    @RequestMapping(value = "/getRuleInfoList", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取产品规则信息")
    public IResponse<IPage<ProductRuleInfo>> listProductRuleInfo(@ModelAttribute ProductRuleInfoCondition plancondition) {
        return IResponse.success(service.page(new Page(plancondition.getPageNumber(), plancondition.getPageSize()), Wrappers.<ProductRuleInfo>query().lambda()
                .eq(ProductRuleInfo::getFitScope, plancondition.getFitScope())
                .eq(ProductRuleInfo::getPlanId, plancondition.getPlanId())
                .orderByAsc(ProductRuleInfo::getCreateTime)
        ));
    }

    @RequestMapping(value = "/getRuleInfoListForce", method = RequestMethod.POST)
    @ApiOperation(value = "多条件分页获取产品规则信息")
    public IResponse<IPage<ProductRuleInfo>> listProductRuleInfoForce(@RequestBody QueryCondition<ProductRuleInfoCondition> plancondition) {
        return IResponse.success(service.page(new Page(plancondition.getPageNumber(), plancondition.getPageSize()), Wrappers.<ProductRuleInfo>query().lambda()
                .eq(ProductRuleInfo::getFitScope, plancondition.getCondition().getFitScope())
                .like(StringUtils.isNotEmpty(plancondition.getCondition().getRuleName()), ProductRuleInfo::getRuleName, plancondition.getCondition().getRuleName())
                .eq(StringUtils.isNotEmpty(plancondition.getCondition().getRuleStatus()), ProductRuleInfo::getRuleStatus, plancondition.getCondition().getRuleStatus())
                .orderByDesc(ProductRuleInfo::getCreateTime)
        ));
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增产品规则")
    public IResponse<Boolean> add(@RequestBody ProductRuleInfo ProductRuleInfo) {
        service.save(ProductRuleInfo);
        return new IResponse<Boolean>().setMsg("新增产品规则成功！");
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改产品规则")
    public IResponse<Boolean> edit(@RequestBody ProductRuleInfo ProductRuleInfo) {
        service.updateById(ProductRuleInfo);
        return new IResponse<Boolean>().setMsg("修改产品规则成功！");
    }

    @RequestMapping(value = "/delById/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除产品规则")
    public IResponse<Boolean> del(@PathVariable String id) {
        ProductRuleInfo productRuleInfo = service.getById(id);
        service.delProductRuleInfo(productRuleInfo);
        return new IResponse<Boolean>().setMsg("删除产品规则成功！");
    }

    @RequestMapping(value = "/effective", method = RequestMethod.POST)
    @ApiOperation(value = "设置有效")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> effective(@ModelAttribute ProductRuleInfoCondition plancondition) {
        for (Long id : plancondition.getIds()) {
            ProductRuleInfo productRuleInfo = service.getById(id);
            service.effectiveRule(productRuleInfo);
        }
        return new IResponse<Boolean>().setData(true).setMsg("批量通过id设置数据有效成功");
    }

    @RequestMapping(value = "/invalid", method = RequestMethod.POST)
    @ApiOperation(value = "设置无效")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> invalid(@ModelAttribute ProductRuleInfoCondition plancondition) {
        for (Long id : plancondition.getIds()) {
            ProductRuleInfo productRuleInfo = service.getById(id);
            service.invalid(productRuleInfo);
        }
        return new IResponse<Boolean>().setData(true).setMsg("批量通过id设置数据无效成功");
    }


}
